﻿// -----------------------------------------------------------------------
// <copyright file="ImageDataProvider.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.DataAccess.DataProviders
{
	#region Usings

	using System.Data;
	using System.Linq;

	using MBOP.Core.Entities.Common;
	using MBOP.Core.Interfaces.DataProviders;
	using MBOP.DataAccess.Contexts;
	using MBOP.Helpers.ExceptionHandling.ExceptionHandlers;

	#endregion

	/// <summary>
	/// The <see cref="IImageDataProvider"/> implementer
	/// which provide image data using EF.
	/// </summary>
	public class ImageDataProvider : IImageDataProvider
	{
		#region Constants
		/// <summary>
		/// Represents the image data property name.
		/// </summary>
		private const string PROPERTY_NAME_DATA = "Data";

		#endregion

		#region IImageDataProvider Members

		#region Methods

		/// <summary>
		/// Gets image from database by id.
		/// </summary>
		/// <param name="id">The image id</param>
		/// <returns>The Image.</returns>
		public Image GetImageByID(string id)
		{
			Image requestedImage = new Image();

			try
			{
				using (DataContext context = new DataContext())
				{
					requestedImage = context.Images.Include(PROPERTY_NAME_DATA).FirstOrDefault(image => image.ID == id);
				}
			}
			catch (DataException ex)
			{
				DataAccessExceptionHandler.HandleException(ex);
			}

			return requestedImage;
		}

		#endregion

		#endregion
	}
}
